/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Database;

import Domain.Apartment;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author Min0
 */
public class DBApartment implements IFDBApartment
{

    private Connection con;

    public DBApartment()
    {
        con = DBConnection.getInstance().getDBcon();
    }

    //START private methods for class
    private Apartment BuildApartment(ResultSet results)
    {
        Apartment appObj = new Apartment();
        try
        {
            appObj.setApartmentNo(results.getInt("ApartmentNo"));
            appObj.setAvailable(Boolean.valueOf(results.getString("Available")));
            appObj.setDescription(results.getString("Description"));
            appObj.setPrice(results.getFloat("Price"));
        } catch (Exception e)
        {
            System.out.println("error in building the apartment object\n" + e.getMessage());
        }
        return appObj;
    }

    private String BuildQuery(String wClause)
    {
        String query = "SELECT * FROM Apartment";

        if (wClause.length() > 0)
        {
            query = query + " WHERE " + wClause;
        }

        return query;
    }

    private Apartment singleWhere(String wClause)
    {
        ResultSet results;
        Apartment appObj = new Apartment();
        String query = BuildQuery(wClause);
        System.out.println(query);
        try
        {
            java.sql.Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            results = stmt.executeQuery(query);

            if (results.next())
            {
                appObj = BuildApartment(results);
                //assocaition is to be build
                stmt.close();
            }

        }//end try	
        catch (Exception e)
        {
            System.out.println("Query exception: " + e);
        }
        return appObj;
    }

    private ArrayList<Apartment> miscWhere(String wClause)
    {
        ResultSet results;
        ArrayList<Apartment> list = new ArrayList<Apartment>();

        String query = BuildQuery(wClause);

        try
        { // read the apartment from the database
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            results = stmt.executeQuery(query);

            while (results.next())
            {
                Apartment appObj = new Apartment();
                appObj = BuildApartment(results);
                list.add(appObj);
            }//end while
            stmt.close();
        } catch (Exception e)
        {
            System.out.println("Query exception - select: " + e);
        }
        return list;
    }
    //END private methods for class

    @Override
    public void InsertApartment(Apartment apartment)
    {
        String query = "INSERT INTO Apartment(ApartmentNo,Description,Available,Price) VALUES("
                + apartment.getApartmentNo() + ",'"
                + apartment.getDescription() + "','"
                + apartment.getAvailable() + "',"
                + apartment.getPrice() + ")";

        System.out.println("insert: " + query);
        try
        {
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        }//end try	
        catch (Exception ex)
        {
            System.out.println("Insert exception in apartment db: " + ex);
        }
    }

    @Override
    public void InsertApartment(int ApartmentNo, String Description, String Available, float Price)
    {
        String query = "INSERT INTO Apartment(ApartmentNo,Description,Available,Price) VALUES("
                + ApartmentNo + ",'"
                + Description + "','"
                + Available + "',"
                + Price + ")";

        System.out.println("insert: " + query);
        try
        {
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        }//end try	
        catch (Exception ex)
        {
            System.out.println("Insert exception in apartment db: " + ex);
        }
    }

    @Override
    public Apartment GetApartment(int appartmentNo)
    {
        String wClause = "ApartmentNo =" + appartmentNo;
        return singleWhere(wClause);
    }

    @Override
    public ArrayList<Apartment> GetApartmentList()
    {
        return miscWhere("");
    }

    @Override
    public void UpdateApartment(int ApartmentNo, String Description, String Available, float Price)
    {
        String query = "UPDATE Apartment SET "
                + "Description ='" + Description + "', "
                + "Available ='" + Available + "', "
                + "Price = " + Price
                + " WHERE ApartmentNo = " + ApartmentNo;
        System.out.println("Update query:" + query);
        try
        {
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        } catch (Exception ex)
        {
            System.out.println("Update exception in apartment db: " + ex);
        }
    }

    @Override
    public void DeleteApartment(int apartmentNo)
    {
        String query = "DELETE FROM Apartment WHERE ApartmentNo = "
                + apartmentNo;
        System.out.println(query);
        try
        { // delete from apartment
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        } catch (Exception ex)
        {
            System.out.println("Delete exception in apartment db: " + ex);
        }
    }

    @Override
    public void UpdateApartmentDescription(int ApartmentNo, String Description)
    {
        String query = "UPDATE Apartment SET "
                + "Description ='" + Description + "'"
                + " WHERE ApartmentNo = " + ApartmentNo;
        System.out.println("Update query:" + query);
        try
        {
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        } catch (Exception ex)
        {
            System.out.println("Update exception in apartment db: " + ex);
        }
    }

    @Override
    public void UpdateApartmentStatus(int ApartmentNo, String Available)
    {
        String query = "UPDATE Apartment SET "
                + "Available ='" + Available + "'"
                + " WHERE ApartmentNo = " + ApartmentNo;
        System.out.println("Update query:" + query);
        try
        {
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        } catch (Exception ex)
        {
            System.out.println("Update exception in apartment db: " + ex);
        }
    }

    @Override
    public void UpdateApartmentPrice(int ApartmentNo, float Price)
    {
        String query = "UPDATE Apartment SET "
                + "Price = " + Price
                + " WHERE ApartmentNo = " + ApartmentNo;
        System.out.println("Update query:" + query);
        try
        {
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        } catch (Exception ex)
        {
            System.out.println("Update exception in apartment db: " + ex);
        }
    }

    @Override
    public int getMax()
    {
        int max = getMax.getMaxId("Select max(ApartmentNo) from Apartment");
        return max;
    }
}
